from flask import Flask, request, jsonify
app = Flask(__name__)

@app.route('/filter', methods=['POST'])
def filter():
    payload = request.json
    nodeNames = [node['name'] for node in payload['nodes']]
    filteredNodes = []

    for nodeName in nodeNames:
        # 假设有一个函数 getNodeLabels(nodeName) 可以获取节点标签
        labels = getNodeLabels(nodeName)
        if 'special-label' in labels and labels['special-label'] == 'true':
            filteredNodes.append({
                'name': nodeName,
                'status': 'Pass'
            })
        else:
            filteredNodes.append({
                'name': nodeName,
                'status': 'Fail'
            })

    return jsonify({'apiVersion': 'v1', 'items': filteredNodes})

@app.route('/prioritize', methods=['POST'])
def prioritize():
    payload = request.json
    nodeNames = [node['name'] for node in payload['nodes']]
    scores = []

    for nodeName in nodeNames:
        # 假设有一个函数 getNodeLabels(nodeName) 可以获取节点标签
        labels = getNodeLabels(nodeName)
        score = 0
        if 'special-label' in labels and labels['special-label'] == 'true':
            score = 10  # 给特殊标签的节点更高评分
        scores.append({
            'name': nodeName,
            'score': score
        })

    return jsonify({'apiVersion': 'v1', 'scores': scores})

# 模拟获取节点标签的函数
def getNodeLabels(nodeName):
    # 实际应用中应该与Kubernetes API交互获取真实标签
    # 这里只是返回一些模拟数据
    return {
        'node-a': {'special-label': 'true'},
        'node-b': {'special-label': 'false'}
    }.get(nodeName, {})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)